package org.rency.common.trigger.listeners;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.Trigger.CompletedExecutionInstruction;
import org.quartz.TriggerListener;
import org.rency.common.trigger.beans.TaskStatusEnum;
import org.rency.common.trigger.schedule.repository.DaemonTaskTriggerDO;
import org.rency.common.trigger.schedule.repository.DaemonTaskTriggerRepository;
import org.rency.common.trigger.util.TriggerDict;
import org.rency.common.utils.tool.DateUtils;
import org.rency.common.utils.tool.InetUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component("daemonTriggerListener")
public class DaemonTriggerListener implements TriggerListener {
	
	private static final Logger logger = LoggerFactory.getLogger(DaemonTriggerListener.class);
	
	private static final long MAX_EXECUTE_MINUTE = 30;
	
	@Resource(name="daemonTaskTriggerRepository")
	private DaemonTaskTriggerRepository daemonTaskTriggerRepository;
	
	@Override
	public String getName() {
		return "DaemonTriggerListener";
	}

	@Override
	public void triggerFired(Trigger trigger, JobExecutionContext context) {
		Integer triggerId = trigger.getJobDataMap().getInt(TriggerDict.JOB_TRIGGER_ID);
		logger.debug("定时任务队列[{}]中[{}]启动.",TriggerDict.DAEMON_TRIGGER_NAME,trigger.getKey().getName());
		String hostName = StringUtils.isBlank(InetUtils.getLocalHost()) ? TriggerDict.UNKNOW_HOST : InetUtils.getLocalHost();
		String localAddr = StringUtils.isBlank(InetUtils.getLocalAddr()) ? TriggerDict.UNKNOW_HOST : InetUtils.getLocalAddr();
		daemonTaskTriggerRepository.updateStatusMachineFire(triggerId,TaskStatusEnum.PROCESS,TaskStatusEnum.NORMAL,hostName,localAddr,trigger.getPreviousFireTime(),trigger.getNextFireTime());
	}

	@Override
	public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
		logger.debug("触发器[" + trigger.getKey().getName() + "]被激活！！！");
		Integer triggerId = trigger.getJobDataMap().getInt(TriggerDict.JOB_TRIGGER_ID);
		DaemonTaskTriggerDO daemon = daemonTaskTriggerRepository.find(triggerId);
		if(TaskStatusEnum.PROCESS == TaskStatusEnum.get(daemon.getStatus()) && DateUtils.isBeforeNow(DateUtils.addMinutes(daemon.getUpdateTime(), MAX_EXECUTE_MINUTE))){
			triggerComplete(trigger,context,CompletedExecutionInstruction.SET_TRIGGER_COMPLETE);
			logger.debug("触发器[" + trigger.getKey().getName() + "]被驳回！！！");
			return true;
		}
		return false;
	}

	@Override
	public void triggerMisfired(Trigger trigger) {
		logger.warn("触发器[" + trigger.getKey().getName() + "]错过执行！！！");
	}

	@Override
	public void triggerComplete(Trigger trigger, JobExecutionContext context,
			CompletedExecutionInstruction triggerInstructionCode) {
		Integer triggerId = trigger.getJobDataMap().getInt(TriggerDict.JOB_TRIGGER_ID);
		logger.debug("定时任务队列[{}]中的[{}]完成.",TriggerDict.DAEMON_TRIGGER_NAME,trigger.getKey().getName());
		daemonTaskTriggerRepository.updateStatusById(triggerId, TaskStatusEnum.NORMAL,TaskStatusEnum.PROCESS);
	}

}
